package controller.Supper;

import com.alibaba.fastjson.JSON;
import pojo.BigClass;
import service.Supper.SuperBigService;
import service.Supper.SuperCustomerService;
import service.Supper.impl.SuperBigServiceImpl;
import service.Supper.impl.SuperCustomerServiceImpl;
import util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 大商品类Servlet
 */
@WebServlet(name = "BigServlet",urlPatterns = "/doBig")
public class BigServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        SuperBigService sbs = new SuperBigServiceImpl();
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        int pageSize = 3;
        if(action==null){
            action="bigQuery";
        }
        if("bigQuery".equals(action)){   //查询大商品类信息
          //  int currentPage = Integer.valueOf(request.getParameter("currentPage"));
           // System.out.println(currentPage);
            //request.getRequestDispatcher("page/bigclass/bigQuery.jsp").forward(request,response);
            response.sendRedirect("/page/bigclass/bigQuery.jsp");
        }else if("BigQuerySell".equals(action)){  //查询大分类信息
            PageSupport pageSupport = new PageSupport();
            int currentPage = Integer.valueOf(request.getParameter("currentPage"));
            int totatCount = sbs.getTotalCount();
            pageSupport.setPageSize(pageSize);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totatCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<BigClass> bigClasses = sbs.selSbig(currentPage,pageSize);
            pageSupport.setList(bigClasses);
            String jsonList = JSON.toJSONString(pageSupport);
            out.write(jsonList);
        }else if("delSbig".equals(action)){  //删除
            Long id = Long.valueOf(request.getParameter("id"));
            int n = sbs.delSbig(id);
            boolean flag = false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("addSbig".equals(action)){  //添加大分类
            BigClass big = new BigClass();
            big.setBigName(request.getParameter("bigName"));
            big.setBigText(request.getParameter("bigText"));
            int n = sbs.addSbig(big);
            boolean flag = false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("selByID".equals(action)){   //根据id进行查询
            Long id = Long.valueOf(request.getParameter("id"));
            List<BigClass> bigClasses = sbs.selSbjgById(id);
            String listJson = JSON.toJSONString(bigClasses);
            out.write(listJson);
        }else if("bigUpdate".equals(action)){  //修改
            BigClass big = new BigClass();
            Long id = Long.valueOf(request.getParameter("id"));
            big.setBigName(request.getParameter("bigName"));
            big.setBigText(request.getParameter("bigText"));
            big.setBigId(id);
            int n = sbs.updSbig(big);
            boolean flag = false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("checkBigName".equals(action)){   //验证是否存在相同的大分类昵称
            String bigName = request.getParameter("bigName");
            int n = sbs.checkBigName(bigName);
            boolean flag = false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
